/**
 * 
 */
Ext.Ajax.timeout = 60000;
Ext.QuickTips.init();

var Patient_View = function(p){
	var v ={
		id:'p_p'+p['id'],
		border:false,
		width : 145,
		height: 105,
		bodyStyle : zurl("background:url(/static/images/expr/bg1.svg.png) no-repeat ;cursor:pointer;"),
		layout:'absolute',
		items:[
			new Ext.ux.Image ({ 
				x: 20,
        		y: 5,
				id: 'imgPreview',
				width:120,
        		height:20, 
				url: zurl('/static/images/expr/white_line.svg.png') 
			}),
			{
				xtype:'label',
				x:20,
				y:5,
				text:p['bed']
			},
			{
				xtype:'label',
				x:20,
				y:27,
				style:'color:blue',
				text:p['name']
			},
			{
				xtype:'label',
				x:20,
				y:47,
				text:p['id']
			},
			{
				xtype:'label',
				x:90,
				y:47,
				text:p['age']
			}
		],
		listeners: {render: 
			function(c) {
				c.el.on('click', function() { 
					//Ext.Msg.alert('Click',p['id']+p['name']);
					if(current_patient != null){
						try{
						current_patient.body.setStyle('background-image', zurl('url(/static/images/expr/bg1.svg.png)'));
						}catch(err){}
					}
					current_patient = c;
					current_patient.body.setStyle('background-image', zurl('url(/static/images/expr/bg2.svg.png)'));
					current_data['patient']=p;
					//设置右上病人信息面板
					right_on_patient_change(p)
//					 var t = Ext.ComponentMgr.get('top_patient_bed');
//					 t.setValue(p['bed']);
//					 var t = Ext.ComponentMgr.get('top_patient_id');
//					 t.setValue(p['id']);
//					 var t = Ext.ComponentMgr.get('top_patient_name');
//					 t.setValue(p['name']);
					
				});
			}
		}
	};
	return v;	
}



var title_panel = {
		width  : 740,
		height : 90,
		region: 'north',		
    	//minSize: 90,
    	//maxSize: 90,
    	margins: '5 5 5 5',
		bodyStyle : zurl("background:url(/static/images/expr/kzt_bt.jpg);")
};



var filter_panel = {
		xtype:'form',
		//width:740,
		height:90,
		region:'north',
		//margins:'5 5 5 5',
		//layout:'table',
		//layoutConfig: {
    	//	columns: 3
		//},
		items:[
		       {
		    	   xtype:'combo',
		    	   fieldLabel:'Patient Type',
		    	   id:'combo_patient_type',
		    	  // width:'100',
		           //forceSelection: true,
		           minChars: 1,
		    	   store:new Ext.data.ArrayStore({
		    			fields:['name'],
		    			proxy : new Ext.data.MemoryProxy([['1']]),
		    			listeners: {
		    				load: function() {
		    					var c=Ext.ComponentMgr.get('combo_patient_type');
		    					var r=this.getAt(0);
		    					var v=r.get('name');
		    					c.setValue(v);
		    					c.fireEvent('select',c,r,0);
		    				}
		    		   }	
		    		}),
		    	   valueField:'name',
		    	   displayField:'name',
		    	   triggerAction: 'all',
		    	   listeners:{
		    		   select:function(c,r,i){
		    			    //console.log('\t'+c+'\t'+r+i);
		    			    current_data['type']=r;
		    			    var value = this.value;
		    			    //console.log('select'+value);
		    			    var array=raw_patients_data[value]['departments'];
		    			    //console.log(array);
		    			   	var n=Ext.ComponentMgr.get('combo_department');
		    			   	n.store.proxy  = new Ext.data.MemoryProxy(array);
		    			   	n.store.load();
		    				//ds_filterWard.reload({params:this.value});
		    		   }
		    	   },
		    	   editable:false

		       },		       
		       {
		    	   xtype:'combo',
		    	   fieldLabel:'Department',
		    	   id:'combo_department',
		           forceSelection: true,
		           minChars: 1,
		    	   store:new Ext.data.ArrayStore({
		    			idIndex:0,
		    			fields:['id','name'],
		    			proxy : new Ext.data.MemoryProxy([['1','2']]),
		    			listeners: {
		    				load: function() {
		    					var c=Ext.ComponentMgr.get('combo_department');
		    					var r=this.getAt(0);
		    					var v=r.get('id');
		    					c.setValue(v);
		    					c.fireEvent('select',c,r,0);
		    				}
		    		   }	
		    		}),
		    	
		    	   valueField:'id',
		    	   displayField:'name',
		    	   mode:'local',
		    	   triggerAction: 'all',
		    	   editable:false,
		    	   listeners:{
		    		   select:function(c,r,i){
		    			   //console.log('department on select\t'+c+'\t'+r+i);
		    			   current_data['department']=r;
		    			   var type_combo = Ext.ComponentMgr.get('combo_patient_type');
		    			   var type = type_combo.value;
		    			   var value = this.value;
		    			   //console.log('select'+value+':'+type);
		    			   var array=raw_patients_data[type]['wards'][value];
		    			   //console.log(array);
		    			   var c=Ext.ComponentMgr.get('combo_region');
		    			   c.store.proxy  = new Ext.data.MemoryProxy(array);
		    			   c.store.reload();
		    		   }
		    	   }
		    	  
		       },
		       {
		    	   xtype:'combo',
		    	   fieldLabel:'Region',
		    	   id:'combo_region',
		    	  // width:'100',
		           forceSelection: true,
		           minChars: 1,
		           store:new Ext.data.ArrayStore({
		    			idIndex:0,
		    			fields:['id','name'],
		    			proxy : new Ext.data.MemoryProxy([['1','2']]),
		    			listeners: {
		    				load: function() {
		    					
		    					var c=Ext.ComponentMgr.get('combo_region');
		    					var r=this.getAt(0);
		    					var v=r.get('id');
		    					c.setValue(v);
		    					c.fireEvent('select',c,r,0);
		    				}
		    		   }	
		    		}),
		    	   valueField:'id',
		    	   displayField:'name',
		    	   mode:'local',
		    	   triggerAction: 'all',
		    	   editable:false,
		    	   listeners:{
		    		   select:function(c,r,i){
		    			   current_data['ward']=r;
		    			   var render_Mask = new Ext.LoadMask(Ext.getBody(), {  
			                     msg : "Rendering Patient View.Please wait!"  
		    			   }); 
		    			   render_Mask.show();
		    			   var type_combo = Ext.ComponentMgr.get('combo_patient_type');
		    			   var type = type_combo.value;
		    			   var department_combo = Ext.ComponentMgr.get('combo_department');
		    			   var department = department_combo.value;
		    			   var ward = this.value;
		    			   patients =   raw_patients_data[type]['patients'][department][ward];
		    			   
		    			  
		    			   
		    			   quickSort(patients, sortContainer);  
		    			   var cp = Ext.ComponentMgr.get('cp_patients_view');
		    			   var p = Ext.ComponentMgr.get('patients_panel');
		    			   cp.remove(p);
		    			   p = new Ext.Panel(patients_panel );
		    			   cp.add(p);
		    			   for(var i=0;i<patients.length;i++){
		    				   //console.log(patients[i]);
		    				   view = Patient_View(patients[i]);
		    				   //console.log(new Ext.Panel(view));
		    				   p.add(view);
		    			   }
		    			  // p.doLayout();
		    			   
		    			   cp.doLayout();
		    			   render_Mask.hide();
		    			  
		    		   }
		    	   }
		       }
		       ]
		
};

patients_panel = {
		id:'patients_panel',
		title:'Patients:',
		autoScroll:true,
		layout:'table',
		layoutConfig: {
    		columns: 5
		},
		defaults: {
		        // 对每一个子面板都有效applied to each contained panel
		        bodyStyle:'padding:200px'
		},
		items: [
		],
		listeners:{
			clicked: function(c,e){
				Ext.Msg.alert('click','click');
			}
		}	
};//patients_panel

var action_panel = new Ext.Panel({
		rowspan:2,
		bodyStyle : zurl("background:url(/static/images/expr/kzt_bt.jpg);")
});
